package com.wwh.furns.service.impl;

import com.wwh.furns.dao.FurnDAO;
import com.wwh.furns.dao.impl.FurnDAOImpl;
import com.wwh.furns.entity.Furn;
import com.wwh.furns.entity.Page;
import com.wwh.furns.service.FurnService;

import java.util.List;

/**
 * @author Albert
 * @date 2024/5/28-16:00
 * @describe
 */
public class FurnServiceImpl implements FurnService {
    FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public boolean addFurn(Furn furn) {
        int res = furnDAO.addFurn(furn);
        return res != -1;
    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page<Furn> furnPage = new Page<>();

        furnPage.setPageNo(pageNo);
        furnPage.setTotalRow(furnDAO.getTotalRowByName(name));
        furnPage.setPageSize(pageSize);
        int pageTotalCount = furnPage.getTotalRow() / furnPage.getPageSize();
        if(furnPage.getTotalRow() % furnPage.getPageSize() > 0) pageTotalCount++;
        furnPage.setPageTotalCount(pageTotalCount);
        furnPage.setItems(furnDAO.getPageItemsByName((pageNo - 1) * pageSize, pageSize, name));
        return furnPage;
    }

    @Override
    public boolean deleteFurnById(int id) {
        int res = furnDAO.deleteFurnById(id);
        return res == 1;
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        Page<Furn> furnPage = new Page<>();

        furnPage.setPageNo(pageNo);
        furnPage.setTotalRow(furnDAO.getTotalRow());
        furnPage.setPageSize(pageSize);
        int pageTotalCount = furnPage.getTotalRow() / furnPage.getPageSize();
        if(furnPage.getTotalRow() % furnPage.getPageSize() > 0) pageTotalCount++;
        furnPage.setPageTotalCount(pageTotalCount);
        furnPage.setItems(furnDAO.getPageItems((pageNo - 1) * pageSize, pageSize));
        return furnPage;
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

}
